package com.itheima.myxmltest;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws DocumentException {
        //解析xml文件
        //获取解析器
        SAXReader saxReader = new SAXReader();

        //解析xml文件并返回一个Document对象
        File file = new File("myxml\\src\\com\\itheima\\myxmltest\\stu.xml");
        Document document = saxReader.read(file);

        //获取根元素
        Element rootElement = document.getRootElement();

        ArrayList<Student> list = new ArrayList<>();

        //获取所有的component元素
        List<Element> elements = rootElement.elements("student");
        System.out.println(elements.size());
        for (Element e : elements) {
            //继续获取里面的内容
            String id = e.attribute("id").getText();
            String name = e.element("name").getText();
            String age = e.element("age").getText();
            String score = e.element("score").getText();

            list.add(new Student(name,Integer.parseInt(age),Integer.parseInt(score)));
        }

        System.out.println(list);

    }
}
